package app.controller.city;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import app.service.WorkCardService;
import app.service.WorkCardService.WorkCardBean;
import app.utils.DataResponse;
@Controller
public class WorkerCardController {
	
	@Autowired
	@Qualifier("WorkCardService")
	private WorkCardService workCardService;
	
	//该方法用来将工单预添加到工单缓存中
	@RequestMapping(value="/workcard/fac/{facid}/type/{type}")
	public void addWorkCard(@PathVariable("facid") Integer facID,@PathVariable("type") Integer type,HttpSession session,HttpServletResponse response){
		Integer addToCache = workCardService.addToCache(facID, type, session);
		DataResponse.jsonResponse(response, addToCache);
	}
	@RequestMapping(value="/worklist/page/{pageNum}/pagesize/{pageSize}")
	public void workCardList(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,HttpSession session,HttpServletResponse response){
		WorkCardBean workCardList = workCardService.workCardList(pageNum, pageSize, session, response);
		DataResponse.jsonResponse(response, workCardList);
	}
	//该方法用来进行移除缓存中的指定工单 
	@RequestMapping("/workcard/remove/fac/{facid}")
	public void delWorkCardCache(@PathVariable("facid") long fac,HttpServletResponse response,HttpSession session){
		Integer removeWorkCard = workCardService.removeWorkCard(fac, session);
		DataResponse.jsonResponse(response, removeWorkCard);
	}
	//用于在指定工单中添加该工单的执行人
	@RequestMapping("/workcard/facflag/{fac}/installor/{installor}")
	public void doSelectUser(@PathVariable("fac") long workcardflag,@PathVariable("installor") Integer installor, HttpServletResponse response,HttpSession session){
		Integer selectUser = workCardService.selectUser(workcardflag, installor, session);
		DataResponse.jsonResponse(response, selectUser);
	}
	//将工单导出并进行数据库存入处理
	@RequestMapping("/workcard/expotr/excel")
	public void commitWorkCardAndExport(HttpSession session,HttpServletResponse response) throws Exception{
		InputStream commit = workCardService.commitWorkCard(session);
		DataResponse.StreamResponse(response, commit, "workcard_"+new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date())+".xls");
	}
	
	//删除未处理的指定工单
	@RequestMapping("/workcard/del/{id}")
	public void delWorkCard(@PathVariable("id") Integer id,HttpServletResponse response){
		Integer delWorlCard = workCardService.delWorlCard(id);
		DataResponse.jsonResponse(response, delWorlCard);
	}
}
